package com.spx.core.wizards;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;

abstract public class ModelWizardPage<T> extends WizardPage
{

	protected Map<String, Object> properties = new HashMap<String, Object>();
	ComponenentListener componentListener = new ComponenentListener();

	protected ModelWizardPage(String pageName)
	{
		super(pageName);

	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite root)
	{

		
	    Composite control =createPageControlx(root);
		addSubFocusListeners(root);
		setControl(control);
	}

	private void addSubFocusListeners(Composite parent)
	{
		for (Control control : parent.getChildren())
		{
			control.addFocusListener(componentListener);

			if (control instanceof ISelectionProvider)
			{
				((ISelectionProvider) control).addSelectionChangedListener(componentListener);
			}

			if (control instanceof Composite)
			{
				addSubFocusListeners((Composite) control);
			}

			if (control instanceof Tree)
			{
				((Tree) control).addSelectionListener(componentListener);
			}
			if (control instanceof List)
			{
				((List) control).addSelectionListener(componentListener);
			}
		}
	}

	@Override
	public boolean isPageComplete()
	{
		boolean result = isValid() && super.isPageComplete();
		return result;
	}

	private class ComponenentListener implements FocusListener, ISelectionListener, ISelectionChangedListener, SelectionListener
	{

		private void exec()
		{
			isValid();
			getWizard().getContainer().updateButtons();
		}

		@Override
		public void focusGained(FocusEvent e)
		{
			exec();
		}

		@Override
		public void focusLost(FocusEvent e)
		{
			exec();
		}

		@Override
		public void selectionChanged(IWorkbenchPart part, ISelection selection)
		{
			exec();
		}

		@Override
		public void selectionChanged(SelectionChangedEvent event)
		{
			exec();
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent arg0)
		{

		}

		@Override
		public void widgetSelected(SelectionEvent arg0)
		{
			exec();

		}

	}

	protected boolean isValid()
	{
		return true;
	}

	protected abstract void createPageControl(Composite parent);
	
	
	protected Composite  createPageControlx(Composite parent)
	{
	    return new Composite(parent,SWT.NULL);
	}
    

	abstract public void setProperties(T modelObject);

	public Map<String, Object> getProperties()
	{

		return properties;
	}

}
